from fastapi import APIRouter, Query, Path, Form
from utils.response import success,fail
from pydantic import BaseModel,Field

testController = APIRouter()


# http://xxx/api/test/demo1?id=10
# http://xxx/api/test/demo1/10

#查询参数
@testController.get("/demo1", description="查询参数示例")
def test1(id:int = Query(default=10,lt=100)):
    return success("查询参数返回成功", id)
#路径参数
@testController.get("/demo2/{id}", description="路径参数示例")
def test2(id:str=Path(pattern='^a$')):
    return success("路径参数返回成功", id)

class UserData(BaseModel):
    username:str = Field(max_length=10,min_length=2)
    password:str = Field(max_length=10,min_length=2)

#body参数 json
@testController.post("demo3")
def test3(data:UserData):
    return success("成功", data)

#formData
@testController.post("demo4")
def test4(username:str = Form(), password:str=Form()):
    return success("成功") 
